package com.mipo.core.utils;

import org.apache.commons.codec.binary.Hex;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DESUtil {
    public static final String ENCODED_UTF8 = "UTF-8";
    public static final String ENCODED_GBK = "GBK";
    public static final String ENCODED_GB2312 = "GB2312";
    public static final String ENCODED_ISO88591 = "ISO8859-1";
    public static final String ENCODED_ASCII = "ASCII";
    public static final String ENCODED_UNICODE = "UNICODE";
    public static final String CIPHER_INSTANCE_CBC = "DES/CBC/PKCS5Padding";
    public static final String CIPHER_INSTANCE_ECB = "DES/ECB/PKCS5Padding";

    public DESUtil() {
    }

    public static String ENCRYPTMethod(String HexString, String keyStr) {
        String jmstr = "";

        try {
            Object e = null;
            String jqstr = getstrByte(keyStr).substring(0, 8).toUpperCase();
            byte[] e1 = jqstr.getBytes("ASCII");
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(e1);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(e1);
            cipher.init(1, secretKey, iv);
            byte[] theCph = cipher.doFinal(HexString.getBytes("GB2312"));
            jmstr = toHexString(theCph).toUpperCase();
            jmstr = toHexString(theCph);
            return jmstr;
        } catch (Exception var11) {
            var11.printStackTrace();
            return null;
        }
    }

    public static String getstrByte(String str) {
        if(null == str) {
            throw new IllegalArgumentException("str is null!");
        } else {
            MessageDigest messageDigest = getMessageDigest();

            byte[] digest;
            try {
                digest = messageDigest.digest(str.getBytes("ASCII"));
            } catch (UnsupportedEncodingException var4) {
                throw new IllegalStateException("ASCII not supported!");
            }

            return new String(Hex.encodeHex(digest));
        }
    }

    protected static final MessageDigest getMessageDigest() {
        String algorithm = "MD5";

        try {
            return MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException var2) {
            throw new IllegalArgumentException("No such algorithm [" + algorithm + "]");
        }
    }

    public static String ENCRYPTMethod(String HexString, String keyStr, String keyENCODED, String HexStringENCODED, String CipherInstanceType) throws Exception {
        String jmstr = "";

        try {
            Object e = null;
            String jqstr = getstrByte(keyStr).substring(0, 8).toUpperCase();
            byte[] e1 = jqstr.getBytes(keyENCODED);
            Cipher cipher = Cipher.getInstance(CipherInstanceType);
            DESKeySpec desKeySpec = new DESKeySpec(e1);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(e1);
            cipher.init(1, secretKey, iv);
            byte[] theCph = cipher.doFinal(HexString.getBytes(HexStringENCODED));
            jmstr = toHexString(theCph).toUpperCase();
            jmstr = toHexString(theCph);
            return jmstr;
        } catch (Exception var14) {
            var14.printStackTrace();
            return null;
        }
    }

    public static String toHexString(byte[] b) {
        StringBuffer hexString = new StringBuffer();

        for(int i = 0; i < b.length; ++i) {
            String plainText = Integer.toHexString(255 & b[i]);
            if(plainText.length() < 2) {
                plainText = "0" + plainText;
            }

            hexString.append(plainText);
        }

        return hexString.toString();
    }

    public static String decrypt(String message, String key, String keyENCODED, String HexStringENCODED, String CipherInstanceType) throws Exception {
        byte[] bytesrc = convertHexString(message);
        Object theKey = null;
        String jqstr = getstrByte(key).substring(0, 8).toUpperCase();
        byte[] theKey1 = jqstr.getBytes(keyENCODED);
        Cipher cipher = Cipher.getInstance(CipherInstanceType);
        DESKeySpec desKeySpec = new DESKeySpec(theKey1);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(theKey1);
        cipher.init(2, secretKey, iv);
        byte[] retByte = cipher.doFinal(bytesrc);
        return new String(retByte, HexStringENCODED);
    }

    public static String decrypt(String message, String key) {
        try {
            byte[] ex = convertHexString(message);
            Object theKey = null;
            String jqstr = getstrByte(key).substring(0, 8).toUpperCase();
            byte[] theKey1 = jqstr.getBytes("ASCII");
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(theKey1);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(theKey1);
            cipher.init(2, secretKey, iv);
            byte[] retByte = cipher.doFinal(ex);
            return new String(retByte, "GB2312");
        } catch (Exception var11) {
            return null;
        }
    }

    public static byte[] convertHexString(String ss) {
        byte[] digest = new byte[ss.length() / 2];

        for(int i = 0; i < digest.length; ++i) {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte)byteValue;
        }

        return digest;
    }
}

